#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

void test()
{
	vector<int> v{ 1,2,3,4,5,6 };
	auto it = v.begin();
	// 将有效元素个数增加到100个，多出的位置使用8填充，操作期间底层会扩容
	// v.resize(100, 8);
	// reserve的作用就是改变扩容大小但不改变有效元素个数，操作期间可能会引起底层容量改变
	// v.reserve(100);
	// 插入元素期间，可能会引起扩容，而导致原空间被释放
	it = v.insert(v.begin(), 0);
	// v.push_back(8);
	// 给vector重新赋值，可能会引起底层容量改变
	//v.assign(100, 8);
	
	while (it != v.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
}

int main()
{
	//初始化
	vector<int> v1(10, 1);
	for (auto e : v1)
	{
		cout << e << " ";
	}
	cout << endl;

	vector<char> v2(10, 'x');
	for (auto e : v2)
	{
		cout << e << " ";
	}
	cout << endl;

	//其他类型的迭代器初始化
	string s1("hello");
	vector<char> v3(s1.begin(),s1.end());
	for (auto e : v3)
	{
		cout << e << " ";
	}
	cout << endl;

	int a[] = { 77,13,6,2 };
	vector<int> v4(a, a + 4);
	for (auto e : v4)
	{
		cout << e << " ";
	}
	cout << endl;

	//隐式类型转换
	vector<string> name;
	name.push_back("lianyihong");
	for (auto ch : name)
	{
		cout << ch;
	}
	cout << endl;

	//自己类型的迭代器
	vector<int> v5;
	v5.push_back(1);
	v5.push_back(2);
	v5.push_back(3);
	v5.push_back(4);

	for (size_t i = 0; i < v5.size(); i++)
	{
		cout << v5[i] << " ";
	}
	cout << endl;

	vector<int>::iterator v5it = v5.begin();
	while (v5it != v5.end())
	{
		cout << *v5it << " ";
		v5it++;
	}
	cout << endl;


	//vector对象排序
	vector<int> v6;
	v6.push_back(2);
	v6.push_back(5);
	v6.push_back(9);
	v6.push_back(7);
	v6.push_back(3);
	v6.push_back(0);

	for (auto e : v6)
	{
		cout << e << " ";
	}
	cout << endl;

	//升序 < less
	sort(v6.begin(), v6.end());
	for (auto e : v6)
	{
		cout << e << " ";
	}
	cout << endl;

	//降序 > greater
	//sort(v6.rbegin(), v6.rend());
	sort(v6.begin(), v6.end(),greater<int>());
	for (auto e : v6)
	{
		cout << e << " ";
	}
	cout << endl;

	//字符串排序
	string s3("hello");
	sort(s3.begin(), s3.end());
	for (auto e : s3)
	{
		cout << e << " ";
	}
	cout << endl;

	//数组排序
	int aa[] = { 2,5,9,7,3,0 };
	sort(aa, aa + 6);
	for (auto e : aa)
	{
		cout << e << " ";
	}
	cout << endl;

	//扩容机制
	vector<int> v7(10,1);
	int cap = v7.capacity();
	for(int i = 0; i < 100; i++)
	{
		v7.push_back(i);
		if (cap != v7.capacity())
		{
			cout << "now capacity:" << v7.capacity() << endl;
			cap = v7.capacity();
		}
	}

	//修改容量
	//reserve  只会扩容不会改变size 不能用[]插入元素
	vector<int> v8;
	v8.reserve(10);
	for (int i = 0; i < 10; i++)
	{
		v8.push_back(i);
	}
	for (auto e : v8)
	{
		cout << e << " ";
	}
	cout << endl;

	//resize 扩容并改变size 可以用[]插入元素
	vector<int> v9;
	v9.resize(10);
	for (int i = 0; i < 10; i++)
	{
		v9[i] = i;
	}
	for (auto e : v9)
	{
		cout << e << " ";
	}
	cout << endl;

	//assign
	v9.assign(10, 2);
	for (auto e : v9)
	{
		cout << e << " ";
	}
	cout << endl;

	//头删
	string s4("hello");
	vector<char> v11(s4.begin(),s4.end());
	v11.erase(v11.begin(),v11.end());
	for (auto e : v11)
	{
		cout << e << " ";
	}
	cout << endl;

	//头插
	v11.insert(v11.begin(), 5, 'x');
	for (auto e : v11)
	{
		cout << e << " ";
	}
	cout << endl;

	//找到3 并删除
	//算法库 InputIterator find (InputIterator first, InputIterator last, const T& val)
	int arr[] = { 2,5,3,6,8,9,3,7,3 };
	vector<int> v12(arr, arr + sizeof(arr) / sizeof(arr[0]));
	vector<int>::iterator pos = find(v12.begin(), v12.end(),3);
	/*v12.erase(pos);
	for (auto e : v12)
	{
		cout << e << " ";
	}
	cout << endl;*/

	//删除所有3 不能用已删除的位置去调用迭代器 会造成迭代器失效
	while (pos != v12.end())
	{
		pos = v12.erase(pos);
		//不能访问 迭代器已经失效
		cout << *(--pos) << endl;
		pos = find(v12.begin(), v12.end(), 3);

	}
	for (auto e : v12)
	{
		cout << e << " ";
	}
	cout << endl;

	test();

	return 0;
}
